#pragma once
#include "dbng.hpp"
#include "../../DB/DBManager.h"
#include "../Common/Common.h"

#include "include\wrapper\cef_message_router.h"

#include <string>
#include <vector>
#include <memory>
using namespace std;
using namespace ormpp;

struct SerialNum{
    int id;
    string num;
    string version;
    string dev;
    string cpuid;
    string hdid;
    string dateout;
};
REFLECTION(SerialNum, id, num, version, dev, cpuid, hdid, dateout)


class SerialNumManager:public CefRunFileDialogCallback
{
private:
    IMPLEMENT_REFCOUNTING(SerialNumManager);
    static CefRefPtr<SerialNumManager> serialnum;
public:
    SerialNumManager(){}
    ~SerialNumManager(){}

    static bool process(string action, CefRefPtr<CefDictionaryValue> data,CefRefPtr<CefBrowser> browser,
        CefRefPtr<CefMessageRouterBrowserSide::Callback> callback){
        if(action == "import")
        {
            CefString title;
            title.FromWString(L"导入");
            vector<CefString> file_paths;
            
            CEF_REQUIRE_UI_THREAD();
            CefRefPtr<CefBrowserHost> host = browser->GetHost();
            if(host.get()) host->RunFileDialog(FILE_DIALOG_OPEN,title,CefString(),vector<CefString>{L".txt"},serialnum);
            

            callback->Success("{}");           
        }else if(action == "add"){
            SerialNum serialnum=CefDictionary2Struct<SerialNum>(data);
            DBManager::inst()->insert(serialnum);
            callback->Success("{}");
        }else if(action == "query"){
            iguana::string_stream ss;
            vector<SerialNum> item = DBManager::inst()->query<SerialNum>();
            iguana::json::to_json(ss, item);
            callback->Success(ss.str());
        }
        return false;
    }
    virtual void OnFileDialogDismissed(const std::vector<CefString>& file_paths) override {
        if(file_paths.size()>0){

        }
    }
};
